/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.common.utils;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author tangli
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-5-24 上午11:25:44          tangli         1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class ArgumentUtil {
    private final static String SYH = "#SYH#";

    /**
     * 
     */
    public ArgumentUtil() {
    }

    public static Map<String, String> parseToMap(String param) {
        Map<String, String> hash = new HashMap<String, String>();
        if (param == null) {
            return hash;
        }
        param = param.trim();
        param = param.replaceAll("\\\\\"", SYH);
        int index0 = -1;
        int index1 = -1;
        int index2 = -1;
        String paramName;
        String paramValue;
        while (param.length() != 0) {
            index0 = -1;
            index1 = param.indexOf('=');
            index2 = param.indexOf(' ');

            if (index1 > 0) {
                index0 = index1;
                if (index2 > 0 && index0 > index2)
                    index0 = index2;
            } else if (index2 > 0)
                index0 = index2;
            if (index0 > 0) {
                paramName = param.substring(0, index0);
                param = param.substring(index0 + 1).trim();
                if (paramName.charAt(0) == '-') {
                    paramName = paramName.substring(1);
                    if (param.charAt(0) == '"') {
                        index0 = param.indexOf('"', 1);
                        if (index0 != -1) {
                            paramValue = param.substring(1, index0);
                            param = param.substring(index0 + 1).trim();
                            if (paramValue.contains(SYH)) {
                                paramValue = paramValue.replaceAll(SYH, "\"");
                            }
                            hash.put(paramName, paramValue);
                        }
                    } else {
                        index0 = param.indexOf(' ', 1);
                        if (index0 == -1) {
                            paramValue = param.trim();
                            param = "";
                            if (paramValue.contains(SYH)) {
                                paramValue = paramValue.replaceAll(SYH, "\"");
                            }
                            hash.put(paramName, paramValue);
                        } else {
                            paramValue = param.substring(0, index0);
                            param = param.substring(index0).trim();
                            if (paramValue.contains(SYH)) {
                                paramValue = paramValue.replaceAll(SYH, "\"");
                            }
                            hash.put(paramName, paramValue);
                        }
                    }
                }
            } else
                break;
        }
        return hash;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        String str = "-KEY abc -VALUE \"a b c\"";
        System.out.println(ArgumentUtil.parseToMap(str));
        System.out.println("java,aaa".substring(0, 4));
        String aa = "aa\"";
        String bb = aa.replaceAll("\"", "#YH#");
        System.out.println(bb);
    }

}
